// pages/approval/index.js
//获取应用实例
const app = getApp()
let winH, winW;

// app.globalData  可以调用app.js 里面的全局数据
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    StatusBar: app.globalData.StatusBar, //状态栏高度
    CustomBar: app.globalData.CustomBar, //头部nav高度
    scrollHeight: app.globalData.scrollHeight + app.globalData.StatusBar,
    title: "认可大厅", //页面标题
    navTabObj: [{
        name: "认可信息",
        className: 'active',
        contentList: []
      }
      // {
      //   name: "悬赏信息",
      //   className: '',
      //   contentList: []
      // }
    ],
    childHeight: 550,
    scrollY: true,
    reward: false,
    nowTabIndex: 0,
    noData: false,
    totalPage: 1,
    currentPage: 1,
    pageSize: 20,
    isBottom: false
    // fixedTop: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getOffset()
  },
  // 获取认可列表
  getApList() {
    wx.showLoading({
      title: '加载中...',
    })
    const that = this
    const data = {
      userCode: wx.getStorageSync('userCode'),
      pageSize: that.data.pageSize,
      currentPage: that.data.currentPage,
      fromType: ''
    }
    that.setData({
      loading: true
    })
    // console.log(data)
    wx.request({
      url: app.globalData.baseUrl + 'accept/getApproval.do',
      method: "post",
      data: data,
      success(res) {
        // console.log('apList', res)
        var length = res.data.acceptList.length
        const data = res.data
        
        if (res.statusCode === 200) {
          if (length > 0) {
            const list = that.pushData(data.acceptList)
            const currentPage = that.data.currentPage
            const totalPage = that.data.totalPage
            
            that.setData({
              loading: false,
              [`navTabObj[0].contentList`]: list,
              noData: false,
              totalPage: data.page.totalPage
            })
            wx.hideLoading()
          } else {
            that.setData({
              loading: false,
              noData: true
            })
            wx.hideLoading()
          }
        }
      }
    })
  },
  pushData(array) {
    const goodList = this.data.navTabObj[0].contentList
    
    array.forEach(item => {
      goodList.push(item)
    })
    return goodList
  },
  // 是否显示誉点赏赐弹窗
  toggleShow(e) {
    // console.log(e)
    const id = e.currentTarget.dataset.id,
      nowAwardIndex = e.currentTarget.dataset.index;

    this.setData({
      modalVisible: !this.data.modalVisible,
      integralID: id,
      nowAwardIndex: nowAwardIndex
    })
  },
  // 输入荣誉点
  honorChange(e) {
    const that = this,
      value = e.detail.value
    if (value !== '') {
      if (value > 3) {
        that.setData({
          awardHonorPoint: ''
        })
      } else {
        that.setData({
          awardHonorPoint: e.detail.value
        })
      }
    }
  },
  // 提交赏赐的誉点
  award() {
    const that = this
    const userInfo = wx.getStorageSync('userInfo')
    const awardHonorPoint = this.data.awardHonorPoint,
      nowAwardIndex = this.data.nowAwardIndex,
      honor = userInfo.honor
    if (awardHonorPoint === '') {
      wx.showToast({
        title: '没有输入誉点',
        image: '/assets/images/icon/error.png'
      })
      return
    }
    var data = {
      "userCode": wx.getStorageSync('userCode'),
      "id": this.data.integralID,
      "num": this.data.awardHonorPoint * 1
    }
    // console.log(data)
    if (data.num > honor) {
      wx.showToast({
        title: '誉点不够',
        image: '/assets/images/icon/error.png'
      })
    } else {
      wx.request({
        url: app.globalData.baseUrl + 'accept/give.do',
        data: data,
        method: 'post',
        success(res) {
          // console.log('赏赐', res);
          if (res.data.rstype) {
            that.setData({
              ['navTabObj[0].contentList[' + nowAwardIndex + '].isPay']: true,
              modalVisible: false,
              honor: res.data.honor
            })
            wx.showToast({
              title: '提交成功',
            })
          }
        }
      })
    }

  },
  //获取top信息，初始化scrollView的高度
  getOffset() {
    wx.getSystemInfo({
      success: function(res) {
        winH = res.windowHeight;
        winW = res.windowWidth;
      }
    })

  },
  // 监听页面滚动
  // onPageScroll(e) {
  //   // console.log()
  //   var scrollTop = e.scrollTop;
  //   this.setData({
  //     scrollTop: scrollTop
  //   })
  // },
  isTop() {
    // console.log("到顶了")
    this.setData({
      scrollY: true
    })
  },
  //tab点击
  tabClick(e) {
    let tab1 = 'navTabObj[0].className',
      tab2 = 'navTabObj[1].className',
      index = e.target.dataset.index;

    if (e.target.dataset.index === 0) {
      this.setData({
        [tab1]: "active",
        [tab2]: "",
        reward: false,
        noData: false,
        totalPage: 1,
        currentPage: 1,
        ['navTabObj[0].contentList']: [],
        isBottom: false
      })
      this.getApList()
    } else {
      this.setData({
        [tab2]: "active",
        [tab1]: "",
        reward: true,
        nowTabIndex: index,
        noData: false
      })
      this.getRewardList()
    }

  },
  getRewardList() {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'reward/getReward.do',
      data: {
        userCode: wx.getStorageSync('userCode')
      },
      method: 'post',
      success(res) {
        // console.log(res)
        var list = res.data.rewardList
        if (list) {
          if (list.length > 0) {
            that.setData({
              ['navTabObj[1].contentList']: list,
              noData: false
            })
          } else {
            that.setData({
              noData: true
            })
          }
          // console.log(this.data.navTabObj)
        }
      }
    })
  },
  // 页面跳转
  toPage(e) {
    var url = e.currentTarget.dataset.url;
    var id = e.currentTarget.dataset.id;
    if (id) {
      url = url + "?id=" + id
    }
    // console.log(id, url)
    wx.navigateTo({
      url: url,
    })
  },
  // 点赞
  loveIt(e) {

    let index = e.target.dataset.index,
      like = e.target.dataset.like,
      id = e.target.dataset.id,
      that = this;
    if (like === -1) {

      const nowTabIndex = this.data.nowTabIndex;
      // console.log(nowTabIndex)
      const item = "navTabObj[" + nowTabIndex + "].contentList[" + index + "]";
      const nowItem = this.data.navTabObj[nowTabIndex].contentList[index];

      wx.request({
        url: app.globalData.baseUrl + 'accept/giveLike.do',
        method: 'post',
        data: {
          userCode: wx.getStorageSync('userCode'),
          id: id
        },
        success(res) {
          // console.log(res)
          if (res.statusCode === 200) {
            that.setData({
              [item + '.like']: 1,
              [item + '.LIKE_NUM']: nowItem.LIKE_NUM + 1
            })
          }
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var timer = null,
          that = this
          
    that.setData({
      roleId: wx.getStorageSync('userInfo').roleId
    })
    timer = setTimeout(function() {
      that.resetData()
      that.getApList()
      clearTimeout(timer)
      return
    }, 500)
  },
  resetData() {
    this.setData({
      totalPage: 1,
      currentPage: 1,
      ['navTabObj[0].contentList']: []
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const currentPage = this.data.currentPage
    const totalPage = this.data.totalPage
    // console.log(totalPage)
    if (currentPage < totalPage) {
      this.setData({
        currentPage: currentPage + 1
      })
      this.getApList()
    } else {
      this.setData({
        isBottom: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})